package com.liu.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.liu.entity.Department;
import com.liu.entity.Disease;
import com.liu.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Department)表控制层
 *
 * @author makejava
 * @since 2023-02-08 13:54:26
 */
@RestController
@RequestMapping("/department")
@CrossOrigin
@Slf4j
public class DepartmentController {
    /**
     * 科室对象
     */
    private DepartmentService departmentService;

    @Autowired
    @Qualifier("departmentServiceImpl")
    public void setService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String name) {
        pageNum = (pageNum - 1) * pageSize;
        List<Department> data = departmentService.queryAllByLimit(pageNum, pageSize, name);
        Long total = departmentService.count(name);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping("/list")
    public List<Department> findAll() {
        return this.departmentService.findAll();
    }

    /**
     * 根据科室找疾病
     *
     * @param id
     * @return
     */
    @PostMapping("/findDis")
    public List<Disease> findDis(Integer id) {
        List<Disease> dis = departmentService.findDisById(id);
        log.info("id: {}", id);
        return dis;
    }

    /**
     * 根据医院找科室
     *
     * @param id
     * @return
     */
    @PostMapping("/findDep")
    public List<Department> findDep(Integer id) {
        return departmentService.findDepByHos(id);
    }

    /**
     * 添加或更新
     *
     * @param department
     */
    @PostMapping("/edit")
    public int insert(@RequestBody Department department) {
        log.info("当前保存或修改的信息:{}", department.toString());
        //根据传递过来的参数studentId是否存在来决定进入哪个方法
        if (department.getId() != null && department.getId().toString().length() > 0) {
            System.out.println(department.getId());
            return departmentService.update(department);
        } else {
            System.out.println(department.getId());
            return departmentService.insert(department);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public int delete(Integer id) {
        log.info("当前删除数据的id:{}", id);
        return departmentService.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delBatch")
    public int deleteBatch(@RequestBody List<Integer> ids) {
        return departmentService.deleteByIds(ids);
    }

    /**
     * 导出文件
     *
     * @param response
     * @throws Exception
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        //从数据库查询所有数据
        List<Department> list = departmentService.findAll();
        //在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);

        //设置浏览器响应格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset-utf-8");
        String fileName = URLEncoder.encode("科室信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();
    }

    /**
     * 导入文件
     *
     * @param file
     * @throws Exception
     */
    @PostMapping("/import")
    public int imp(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<Department> list = reader.readAll(Department.class);
        System.out.println(list);
        return departmentService.insertBatch(list);
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Department queryById(Integer id) {
        return departmentService.queryById(id);
    }

}

